import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

const fetchProfileAllInfo = (
  isLoading = true,
  rank = 0,
  day = getFormatDate(),
  oday = getFormatDate(),
  oweek = getFormatDate(),
  omonth = getFormatDate(),
) => (dispatch) => {
  dispatch(loading(isLoading));

  Request.get({
    url: `/dealer/saleperformance?rank=${rank}&staff=&day=${day}&oday=${oday}&oweek=${oweek}&omonth=${omonth}`,
    success: (data) => {
      dispatch(receiveProfileAllInfo(data));
    },
    fail: () => {
      dispatch(receiveProfileAllInfo({}));
    },
    error: () => {
      dispatch(receiveProfileAllInfo({}));
    },
  });
};

//  获取格式为"YYYY-MM-DD"的日期
const getFormatDate = () => {
  const today = new Date();
  const year = today.getFullYear();
  const month = today.getMonth() > 8 ? today.getMonth() + 1 : `0${today.getMonth() + 1}`;
  const day = today.getDate() > 9 ? today.getDate() : `0${today.getDate()}`;

  return `${year}-${month}-${day}`;
};

const loading = isLoading => ({
  type: types.FETCH_PROFILEALL_INFO,
  isLoading,
});

const receiveProfileAllInfo = ProfileAllInfo => ({
  type: types.RECEIVE_PROFILEALL_INFO,
  ProfileAllInfo,
});


export default fetchProfileAllInfo;
